package cn.hl.hm.handler;

import java.util.Map;

import com.google.common.collect.Maps;

import cn.hl.hm.constants.PayMethod;
import cn.hl.hm.util.SpringContextUtil;

/**
 * 支付工厂类
 * @author 大和  
 *
 */
public final class PayExecuteFactory {

	private static final Map<PayMethod,Class<? extends PayHandler>> handlers = Maps.newHashMap();

	private PayExecuteFactory(){}
	
	/**
	 * 子类必须调用此方法注册到PayHandler中
	 * @param payMethod
	 * @param clazz
	 */
	static final void register(PayMethod payMethod, Class<? extends PayHandler> clazz) {
		
		if (payMethod==null) {
			throw new NullPointerException("PayMethod is null");
		}
		if (clazz==null) {
			throw new NullPointerException("PayHandler class is null");
		}
		handlers.put(payMethod, clazz);
	}
	
	public static PayHandler findPayHandler(PayMethod payMethod){
		if (payMethod==null) {
			return null;
		}
		Class<? extends PayHandler> clazz = handlers.get(payMethod);
		if (clazz==null) {
			return null;
		}
		String className = clazz.getName();
		return SpringContextUtil.getBean(clazz);
	}
}
